PHP

How to demonstrate the NULL reference exception using exception handling in PHP | PHP programs

How to demonstrate the NULL reference exception using exception handling in PHP | PHP programs

How to demonstrate the NULL reference exception using exception handling in PHP | PHP programs

We will use try, catch, and throw keywords to demonstrate the NULL reference exception in a PHP program.

<?php


class Sample
{
    public function PrintHello()
    {
        printf("Hello World<br>");
    }
}

function GetObject()
{
    $obj = new Sample();
    return $Obj;
}
try
{
    $obj = GetObject();

    if ($obj == null) throw new Exception("Null reference exception");

    $obj->PrintHello();
}
catch(Exception $e)
{
    printf("Exception: %s", $e->getMessage());
}
?>
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community